<!--权限管理-->
<template>
  <div class="app-container">
    <el-button v-waves style="margin: 10px 0;float: right;" class="filter-item" type="primary" icon="el-icon-plus" @click="toAdd(0,'无父权限')">
      添加
    </el-button>
    <el-table
      :data="tableData"
      style="width: 100%;min-height: 45vh"
      row-key="id"
      border
      lazy
      :load="load"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="ID" width="180" align="center"/>
      <el-table-column prop="groupName" label="名称" width="180" align="center"/>
      <el-table-column prop="status" label="状态" :formatter="statusFormat"  align="center"/>
      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="toAdd(scope.row.id,scope.row.groupName)">成员管理</el-button>
          <el-button type="text" icon="el-icon-edit" @click="toAdd(scope.row.id,scope.row.groupName)">添加资源</el-button>
          <el-button circle icon="el-icon-plus" class='filter-item' type="primary" @click="toAdd(scope.row.id,scope.row.groupName)"></el-button>
          <el-button circle  type="primary" icon="el-icon-edit" @click="toEdit(scope.row)"></el-button>
          <el-button circle  type="warning" icon="el-icon-delete" @click="todelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      class="lnk-dialog"
      :close-on-click-modal="false"
      :title="dialogTitle"
      top="10vh"
      width="35%"
      :visible.sync="newDialog"
      :append-to-body="true"
    >
      <el-form
        ref="mainForm"
        :model="formData"
        :rules="rules"
        size="mini"
        label-width="100px"
        class="editForm"
      >
        <!--表单项-->
        <el-row>
          <el-col>
            <el-form-item label="权限名称" prop="groupName" class="car-form-item">
              <el-input v-model="formData.groupName" class="car-modal-col" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="所属权限" prop="parentId" class="car-form-item">
              <el-input v-model="parentName" class="car-modal-col" disabled="true" />
            </el-form-item>
            <el-form-item label="权限状态" prop="status" class="car-form-item">
              <el-select v-model="formData.status" class="car-modal-col" placeholder="请选择状态">
                <el-option v-for="item in statusChoice" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="newDialog = false;$refs['mainForm'].resetFields();">取消</el-button>
        <el-button type="primary" @click="saveData()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
  .table-pagination {
    margin-top: 20px;
    text-align: right;
  }
  ::v-deep .el-table th > .cell{
    padding: 11px!important;
    font-size: 14px;
  }

  ::v-deep .el-table th{
    background-color:#fafafa;
    padding: 0!important;
  }
  ::v-deep .el-table td .cell{
    line-height: 32px;
  }

  ::v-deep .el-table__body tr:nth-child(2n) {
    background-color: #F1F4F9 !important;
  }
  .right-btn {
    float: right;
  }

  .dialog-footer {
    text-align: center;
  }

  .car-modal-col {
    width: 280px;
  }
  .el-table>.has-gutter>tr{
    background-color: #fafafa!important;
    color: #000000!important;
    font-size: 14px!important;
    .cell{
      color: #000000!important;
      font-size: 14px!important;
    }
  }
  ::v-deep .el-table th.is-leaf{
    color: #000!important;
  }
</style>

<script>

export default {
  data() {
    return {
      rules: {
        groupName: [
          { required: true, trigger: 'blur' }
        ],
        status: [
          { required: true, trigger: 'blur' }
        ]
      }, // 表单校验规则
      tableData: [],
      totalCount: '',
      dialogTitle: '', // 弹框名称
      editorFlag: false,
      newDialog: false, // 添加弹框
      formData: {}, // 添加表单
      appId: '',
      pageIndex: 1,
      pageSize: 10,
      listQuery: {},
      rowStyleOption: { // 表格样式
        stripe: true, // 斑马纹
        hoverHighlight: true, // hover高亮
        clickHighlight: true // 点击行高亮
      },
      statusChoice: [
        { value: 1,
          label: '活动'
        },
        { value: 0,
          label: '禁用'
        }
      ]
    }
  },
  computed: {
  },
  created() {
    this.appId = sessionStorage.getItem('appId')
    this.initDatabase()
  },
  methods: {
    /**
     * 设置status显示label
     * @date 2021-05-21
     */
    statusFormat(row, column) {
      if (row.status === 1) {
        return '活动'
      } else {
        return '禁用'
      }
    },
    /**
     * 初始化数据
     * @date 2021-05-10
     */
    async initDatabase() {
      this.tableData = []
      if (this.appId === '') {
        this.appId = sessionStorage.getItem('appId')
      }
      const param = { appId: this.appId }
      const data = await this.$http.post('/auth/roleGroup/list/', param)
      if (data.code === 200) {
        this.tableData = data.data
        this.totalCount = data.data.length
      } else {
        this.$message({
          type: 'error',
          message: '查询失败，请刷新页面'
        })
      }
      console.log(this.tableData)
    },
    /**
     * 删除
     * @param {Object} row
     */
    async todelete(row) {
      const that = this
      this.$alert('是否确认删除', '确认删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const data = await that.$http.delete('/auth/roleGroup/delete/' + row.id, {})
        console.log(data)
        if (data.code === 200) {
          that.initDatabase()
        } else {
          that.$message({
            type: 'info',
            message: '操作失败，请重试'
          })
        }
      })
    },
    /**
     * 点击新增按钮
     * @date 2021-05-08
     */
    toAdd(parentId, parentName) {
      this.formData = { parentId: parentId }
      this.parentName = parentName
      this.newDialog = true
      this.editorFlag = false // 编辑标志
      this.dialogTitle = '新增权限组'
    },
    /**
     * 点击编辑按钮
     * @date 2021-05-08
     */
    toEdit(row) {
      this.formData = row
      this.newDialog = true
      this.editorFlag = false // 编辑标志
      this.dialogTitle = '修改权限组'
    },
    /**
     * 新增/编辑 操作
     * @date 2021-05-08
     */
    async saveData() {
      this.$refs['mainForm'].validate(async(valid) => {
        if (valid) {
          if (this.formData.id) {
            const data = await this.$http.post('/auth/roleGroup/update', this.formData)
            console.log(data)
            if (data.code === 200) {
              this.newDialog = false
              this.initDatabase()
            } else {
              this.$message({
                type: 'info',
                message: '操作失败，请重试'
              })
            }
          } else {
            // this.formData.appId = this.appId;
            console.log(this.formData)
            const data = await this.$http.post('/auth/roleGroup/save', this.formData)
            console.log(data)
            if (data.code === 200) {
              this.newDialog = false
              this.initDatabase()
            } else {
              this.$message({
                type: 'info',
                message: '操作失败，请重试'
              })
            }
          }
        }
      })
    }
  }
}
</script>
